/**
 * 
 */
package com.revenco.bi.dao.nutz;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Repository;

import com.revenco.bi.dao.TnPublicDemensionValueSelDao;
import com.revenco.bi.model.TnPublicDemensionValueSel;

/**
 * @author LingJun
 * @version $Revision:1.0.0, 2014年1月20日 上午11:33:30
 */
@Repository
public class TnPublicDemensionValueSelDaoImpl extends AstractDaoImpl implements
		TnPublicDemensionValueSelDao {

	public List<TnPublicDemensionValueSel> getTnPublicDemensionValueSelByPvId(
			String pvId) {
		Condition condition = Cnd.where("PV_ID", "=", pvId);
		return this.getDao().query(TnPublicDemensionValueSel.class, condition);
	}

	@SuppressWarnings("unchecked")
	public Map<String, String> getDemensionValues(String textColName,
			String valueColName, String table) {
		Sql sql = Sqls
				.create("SELECT $text as text, $value as value FROM $table");
		sql.vars().set("text", textColName);
		sql.vars().set("value", valueColName);
		sql.vars().set("table", table);
		sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				Map<String, String> map = new HashMap<String, String>();
				while (rs.next())
					map.put(rs.getString("text"), rs.getString("value"));
				return map;
			}
		});
		this.getDao().execute(sql);
		return sql.getObject(Map.class);
	}
	
	public QueryResult getDemensionValuesQr(String textColName,
			String valueColName, String table,Pager pager) {
		Condition cnd = Cnd.where("1", "=", 1);
		List<Record> list = this.getDao().query(table, cnd,pager);
		pager.setRecordCount(this.getDao().count(table));
        return new QueryResult(list, pager);
	}
}
